<template>
    <div class="Community-content">
        <div class="title">社区结构分析</div>
        <div class="content-gap flex-between">
            <div class="content-gap-item">
                <div class="gap-item-icon">
                    <!-- <img src="@/assets/icon/community/icon1.png" alt=""> -->
                </div>
                <div class="gap-item-info">
                    <span class="item-count" data-value="467023">0</span>
                    <span class="item-ctry">住宅</span>
                </div>
            </div>
            <div class="content-gap-item ">
                <div class="gap-item-icon">
                    <!-- <img src="@/assets/icon/community/icon1.png" alt=""> -->
                </div>
                <div class="gap-item-info">
                    <span class="item-count" data-value="234567">0</span>
                    <span class="item-ctry">办公楼</span>
                </div>
            </div>
            <div class="content-gap-item">
                <div class="gap-item-icon">
                    <!-- <img src="@/assets/icon/community/icon1.png" alt=""> -->
                </div>
                <div class="gap-item-info">
                    <span class="item-count" data-value="156789">0</span>
                    <span class="item-ctry">园区</span>
                </div>
            </div>
            <div class="content-gap-item ">
                <div class="gap-item-icon">
                    <!-- <img src="@/assets/icon/community/icon1.png" alt=""> -->
                </div>
                <div class="gap-item-info">
                    <span class="item-count" data-value="98765">0</span>
                    <span class="item-ctry">综合体</span>
                </div>
            </div>
            <div class="content-gap-item">
                <div class="gap-item-icon">
                    <!-- <img src="@/assets/icon/community/icon1.png" alt=""> -->
                </div>
                <div class="gap-item-info">
                    <span class="item-count" data-value="345678">0</span>
                    <span class="item-ctry">公寓</span>
                </div>
            </div>
            <div class="content-gap-item ">
                <div class="gap-item-icon">
                    <!-- <img src="@/assets/icon/community/icon1.png" alt=""> -->
                </div>
                <div class="gap-item-info">
                    <span class="item-count" data-value="123456">0</span>
                    <span class="item-ctry">商业街</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, nextTick } from 'vue'

// 数字滚动动画函数
const animateNumber = (element, targetValue, duration = 300) => {
    const startValue = 0
    const startTime = performance.now()

    const updateNumber = (currentTime) => {
        const elapsed = currentTime - startTime
        const progress = Math.min(elapsed / duration, 1)

        // 使用缓动函数让动画更自然
        const easeOutQuart = 1 - Math.pow(1 - progress, 4)
        const currentValue = Math.floor(startValue + (targetValue - startValue) * easeOutQuart)

        // 格式化数字，添加千分位分隔符
        element.textContent = currentValue.toLocaleString()

        if (progress < 1) {
            requestAnimationFrame(updateNumber)
        }
    }

    requestAnimationFrame(updateNumber)
}

// 观察器回调函数
const observerCallback = (entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            const countElement = entry.target.querySelector('.item-count')
            if (countElement) {
                const targetValue = parseInt(countElement.dataset.value)
                animateNumber(countElement, targetValue)
            }
        }
    })
}

onMounted(async () => {
    await nextTick()

    // 创建交叉观察器
    const observer = new IntersectionObserver(observerCallback, {
        threshold: 0.5,
        rootMargin: '0px 0px -50px 0px'
    })

    // 观察所有数字元素
    const countElements = document.querySelectorAll('.content-gap-item')
    countElements.forEach(item => observer.observe(item))
})
</script>
<style scoped lang="scss">
.Community-content {
    width: 100%;
    height: 300px;
    padding: 30px 18px 18px 26px;

    .content-gap {
        width: 92%;
        height: 100%;
        display: flex;
        flex-direction: flex-start;
        align-items: center;
        flex-wrap: wrap;
        margin-top: 0px;
        .content-gap-item {
            width: 51%;
            height: 70px;
            display: flex;
            align-items: center;
            gap: 12px;
            margin-left: -10px;
            opacity: 0;
            transform: translateY(20px);
            animation: fadeInUp 0.6s ease forwards;

            &:nth-child(1) {
                animation-delay: 0.1s;
            }

            &:nth-child(2) {
                animation-delay: 0.2s;
            }

            &:nth-child(3) {
                animation-delay: 0.3s;
            }

            &:nth-child(4) {
                animation-delay: 0.4s;
            }

            &:nth-child(5) {
                animation-delay: 0.5s;
            }

            &:nth-child(6) {
                animation-delay: 0.6s;
            }

            .gap-item-info {
                height: 50%;
                display: flex;
                flex-direction: column;
                justify-content: center;
                gap: 8px;

                .item-count {
                    font-weight: 600;
                    font-size: 18px;
                    color: #00B85C;
                    font-family: 'Courier New', monospace;
                    background: linear-gradient(135deg, #00B85C 0%, #00D4AA 100%);
                    -webkit-background-clip: text;
                    -webkit-text-fill-color: transparent;
                    background-clip: text;
                    text-shadow: 0 2px 4px rgba(0, 184, 92, 0.3);
                    transition: all 0.3s ease;
                }

                .item-ctry {
                    font-weight: 400;
                    font-size: 14px;
                    color: #2E3238;
                    opacity: 0.7;
                    font-weight: 微软雅黑;
                }
            }

            .gap-item-icon {
                width: 40px;
                height: 40px;
                border-radius: 10px;
                background: linear-gradient(135deg, #1872F9 0%, #4A90E2 100%);
                box-shadow: 0 4px 12px rgba(24, 114, 249, 0.3);
                transition: all 0.3s ease;

                &:hover {
                    transform: translateY(-2px);
                    box-shadow: 0 6px 16px rgba(24, 114, 249, 0.4);
                }
            }
        }
    }

    .title {
        color: #141414;
        font-family: Alimama FangYuanTi VF;
        font-weight: normal;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: bold;
        font-size: 20px;
        color: #383C42;
        // 左边蓝色的线
        // 设置间距
        position: relative;

        &::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 100%;
            background-color: #1872F9;
            border-radius: 10px;
            margin-left: -10px;
            margin-top: 1px;
        }

    }

    // 入场动画关键帧
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(20px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
}
</style>